package com.xhh.nphm.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.nphm.system.domain.MWorkcsbase;
import com.xhh.nphm.system.domain.MmailWorkForm;
import com.xhh.nphm.system.domain.TWorkcsbase;
import com.xhh.nphm.system.mapper.MWorkcsbaseDao;
import com.xhh.nphm.system.mapper.TWorkcsbaseDao;
import com.xhh.nphm.system.service.MWorkcsbaseService;
import com.xhh.nphm.system.service.TWorkcsbaseService;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
* @author Administrator
* @description 针对表【tm_telCopy(电话工单抄送件表)】的数据库操作Service实现
* @createDate 2022-07-21 16:14:27
*/
@Service
public class MWorkcsbaseServiceImpl extends ServiceImpl<MWorkcsbaseDao, MWorkcsbase>
    implements MWorkcsbaseService {

    /**
     * 新增抄送件
     *
     * @param wfId    被抄送的工单编号
     * @param deptNos 查看抄送件的部门
     * @param deptNames 部门名称
     * @return 影响行数
     */
    @Override
    public int copy(Integer wfId, String deptNos, String deptNames) {
        int flag = 0;
        //查询数据库是否存在相同件
        QueryWrapper<MWorkcsbase> queryWrapper = Wrappers.query();
        int sameFlag = 1;
        //如果查看抄送件的部门是多个
        if (deptNos.indexOf(',') != -1) {
            String[] deptNo = deptNos.split(",");
            String[] deptName = deptNames.split(",");
            for (int i = 0;i < deptNo.length;i++) {
                queryWrapper.eq("wfId",wfId);
                queryWrapper.eq("deptId",Integer.parseInt(deptNo[i]));
                sameFlag = getBaseMapper().selectCount(queryWrapper);
                if(sameFlag == 0){
                    flag = getBaseMapper().insert(new MWorkcsbase(wfId,Integer.parseInt(deptNo[i]),deptName[i]));
                }
                queryWrapper.clear();
            }
        }
        //一个
        else{
            queryWrapper.eq("wfId",wfId);
            queryWrapper.eq("deptId",Integer.parseInt(deptNos));
            sameFlag = getBaseMapper().selectCount(queryWrapper);
            if(sameFlag == 0){
                flag = getBaseMapper().insert(new MWorkcsbase(wfId,Integer.parseInt(deptNos),deptNames));
            }
        }
        return flag;
    }

    /**
     * 获取抄送件的所有所属部门编号
     *
     * @param wfId 抄送件的工单编号
     * @return 部门编号字符串（用逗号分割）
     */
    @Override
    public String getDeptNos(Integer wfId) {
        //返回的部门编号字符串
        StringBuilder deptNos = new StringBuilder();
        List<MWorkcsbase> list = getList(wfId);
        Iterator<MWorkcsbase> it = list.iterator();
        while (it.hasNext()) {
            MWorkcsbase item = it.next();
            deptNos.append(item.getDeptId());
            if (it.hasNext())
                deptNos.append(",");
            else
                break;
        };
        return deptNos.toString();
    }

    /**
     * 获取抄送件的所有所属部门名称
     *
     * @param wfId 抄送件的工单编号
     * @return 部门名称字符串（用逗号分割）
     */
    @Override
    public String getDeptNames(Integer wfId) {
        //返回的部门编号字符串
        StringBuilder deptNames = new StringBuilder();
        List<MWorkcsbase> list = getList(wfId);
        Iterator<MWorkcsbase> it = list.iterator();
        while (it.hasNext()) {
            MWorkcsbase item = it.next();
            deptNames.append(item.getDeptName());
            if (it.hasNext())
                deptNames.append(",");
            else
                break;
        };
        return deptNames.toString();
    }

    /**
     * 根据工单编号查询抄送件
     * @param wfId 工单编号
     * @return 抄送件列表
     */
    @Override
    public List<MWorkcsbase> getList(Integer wfId) {
        QueryWrapper<MWorkcsbase> queryWrapper = Wrappers.query();
        queryWrapper.eq("wfId", wfId);
        //该工单的所有抄送件记录
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<MmailWorkForm> mailCopyList(MmailWorkForm mmailWorkForm, Long deptId) {
        return getBaseMapper().mailCopyList(mmailWorkForm,deptId);
    }
}




